<template>
  <div>
    <el-container>
      <!-- <el-aside width="300px">
        <el-card :style="`height:${height}px`" class="box-card">
          <div slot="header" class="clearfix">
            <span>功能菜单</span>
          </div>
          <leftTree treeType="2"></leftTree>
        </el-card>
      </el-aside> -->
      <el-main>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>权限资源管理</span>
          </div>
          <el-form size="mini" :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="权限名称">
              <el-input clearable v-model="formInline.user" placeholder="权限名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
              <!-- <el-button type="primary" @click="authorize">批量授权</el-button> -->
            </el-form-item>
          </el-form>
          <el-table @selection-change="changeFun" :data="tableData" :height="heightTab" border style="width: 100%">
            <el-table-column type="selection" width="40"></el-table-column>
            <el-table-column prop="checked" label="授权">
              <template slot-scope="scope">
                  <span v-if="scope.row.checked == '1'">已授权</span>
                  <span v-else>未授权</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button v-if="scope.row.checked == '1'" type="text" size="small" @click="cancelAuthorize(scope.row)">取消授权</el-button>
                <el-button v-else type="text" size="small" @click="authorize(scope.row)">授权</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-pagination
            class="block"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="400"
          ></el-pagination> -->
        </el-card>
      </el-main>
    </el-container>
  </div>
</template>
<script>
const leftTree = () => import("@/components/leftTree");
const SelectTree = () => import("@/components/SelectTree");
export default {
  data() {
    return {
      height: window.innerHeight - 140,
      heightTab: window.innerHeight - 310,
      formInline: {
        user: "",
        region: "",
      },
      tableData: [],
      currentPage: 1,
      dialogVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: "",
        resource: "",
        desc: "",
      },
      defaultProps: {
        parent: "parentId", // 父级唯一标识
        value: "id", // 唯一标识
        label: "label", // 标签显示
        children: "children", // 子级
      },
      // 数据列表
      options: [
        {
          parentId: "0",
          id: "A",
          label: "label-A",
          children: [
            {
              parentId: "A",
              id: "A-1",
              label: "label-A-1",
            },
          ],
        },
        {
          parentId: "1",
          value: "B",
          label: "label-B",
          children: [],
        },
      ],
      tableDataAll:[]
    };
  },
  components: {
    leftTree: leftTree,
    "select-tree": SelectTree,
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleChange(value) {
      console.log(value);
      console.log(this.form);
    },
    treeNode(data) {
      console.log(data);
    },
    getPermission(){
      this.$ajax.get("/admin/getpermission").then((res) => {
        this.tableData = res.data;
        this.tableDataAll = res.data
      });
    },
    changeFun(data){
      console.log(data);
    },
    authorize(data){
      data['checked'] = '1'
      this.$ajax.post("/admin/updatepermission",data).then((res) => {
        this.getPermission()
      });
    },
    cancelAuthorize(data){
      data['checked'] = '0'
      this.$ajax.post("/admin/updatepermission",data).then((res) => {
        console.log(res);
        this.getPermission()
      });
    },
    search(){
      if (this.formInline.user != "") {
        this.tableData = []
        this.tableDataAll.forEach(item => {
          if (item.name.indexOf(this.formInline.user) != -1) {
            this.tableData.push(item)
          }
        })
      }else{
        this.tableData = this.tableDataAll
      }
    }
  },
  mounted(){
    this.getPermission()
  }
};
</script>
<style lang="less" scoped>
.block {
  padding: 10px 0;
}
/deep/ .el-card__body {
  padding-bottom: 0;
}
</style>